<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/2/21
 * Time: 17:47
 */

namespace app\api\controller;

use app\api\model\GoodsCate AS GoodsCateModel;

class Goodscate extends Base
{
    public function __construct()
    {
        parent::__construct();

    }

    /**
     * 商品分类---三级
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function goodsCateList(){
        $field = 'cate_id, cate_name_en, cate_name_es, cate_name_tp, cate_pid';
        $firstGoodsCate = GoodsCateModel::selectGoodsCate(['cate_level' => 1], $field);
        $secondGoodsCate = GoodsCateModel::selectGoodsCate(['cate_level' => 2], $field);
        $thirdGoodsCate = GoodsCateModel::selectGoodsCate(['cate_level' => 3], $field);

        if($firstGoodsCate){
            if($secondGoodsCate){
                foreach ($secondGoodsCate as $ks => $vs){
                    if($thirdGoodsCate){
                        $_thirdGoodsCate = [];
                        foreach($thirdGoodsCate as $kt => $vt){
                            if($vt['cate_pid'] == $vs['cate_id']){
                                $_thirdGoodsCate[] = $vt;
                            }
                        }
                        $secondGoodsCate[$ks]['thirdGoodsCate'] = $_thirdGoodsCate;
                    }else{
                        $secondGoodsCate[$ks]['thirdGoodsCate'] = [];
                    }
                }
            }else{
                foreach ($firstGoodsCate as $kf => $vf){
                    $firstGoodsCate[$kf]['secondGoodsCate'] = [];
                }
                return json(['status' => 200, 'msg' => $firstGoodsCate]);
            }

            foreach ($firstGoodsCate as $kf1 => $vf1){
                $_secondGoodsCate = [];
                foreach ($secondGoodsCate as $ks1 => $vs1){
                    if($vs1['cate_pid'] == $vf1['cate_id']){
                        $_secondGoodsCate[] = $vs1;
                    }
                }
                $firstGoodsCate[$kf1]['secondGoodsCate'] = $_secondGoodsCate;
            }

            return json(['status' => 200, 'msg' => $firstGoodsCate]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }

    }

    /**
     * 首页一级分类
     * type    1 全部    2 推荐
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function firstGoodsCate(){
        $type = input('type') ? input('type') : 1;

        $where['cate_level'] = 1;
        if($type == 2) $where['cate_recommend'] = 1;

        $field = 'cate_id, cate_name_en, cate_name_es, cate_name_tp, cate_image';
        $firstGoodsCate = GoodsCateModel::selectGoodsCate($where, $field);
        return json(['status' => 200, 'msg' => $firstGoodsCate]);
    }
}